/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Operations;

import hep.aida.*;
import java.awt.image.BufferedImage;

/**
 *
 * @author littlechild
 */
public class CmputHistogramInMode {
    String mode;
    public IHistogram compute(BufferedImage image) {
        //check the mode and initiate the ForEachPixel function
        FillHistogram fh=new FillHistogram();
        if(mode.equals("Gray")) {
            fh.mode=new GrayMode();
        }else if(mode.equals("Blue")) {
            fh.mode=new BlueMode();
        }else if(mode.equals("Green")) {
            fh.mode=new GreenMode();
        }else if(mode.equals("Red")) {
            fh.mode=new RedMode();
        }else { // can not reach here
            assert false;
        }
        //intiate the histogram to fill
        IAnalysisFactory af=IAnalysisFactory.create();
        ITree tree = af.createTreeFactory().create();
        IHistogramFactory hf = af.createHistogramFactory(tree);
        IHistogram1D histogram=hf.createHistogram1D("直方图", 256,0,255);
        //fun the For each Pixel function
        fh.conduct(image, histogram);
        return histogram;
    }
    public void setMode(String m) {
        mode=m;
    }
}

interface Mode {
    int getValue(BufferedImage image,int x, int y);
}

class FillHistogram {
    Mode mode;
    public void conduct(BufferedImage input, IHistogram1D output) {
        for(int i=0;i!=input.getWidth();++i) {
            for(int j=0;j!=input.getHeight();++j) {
                output.fill(mode.getValue(input, i, j));
            }
        }
    }
}


class GrayMode implements Mode {

    public int getValue(BufferedImage image, int x, int y) {
        int rgb=image.getRGB(x, y);
        return CommonFunc.getGray(rgb);
    }
}

class BlueMode implements Mode {

    public int getValue(BufferedImage image, int x, int y) {
        int rgb=image.getRGB(x, y);
        return CommonFunc.getBlue(rgb);
    }
    
}

class RedMode implements Mode {
    public int getValue(BufferedImage image, int x, int y) {
        int rgb=image.getRGB(x, y);
        return CommonFunc.getRed(rgb);
    }
}

class GreenMode implements Mode {
    public int getValue(BufferedImage image, int x, int y) {
        int rgb=image.getRGB(x, y);
        return CommonFunc.getGreen(rgb);
    }
}